/*1.业务逻辑(左侧加号展开阅读):
 * 	本程序用于用户访问首页，访问分为2种情况：
 *		1) 当用户首次访问该servlet时，跳转到登录界面
 *		2) 当用户再次访问该servlet时，直接跳转至首页
 *	当session过期，或用户自行退出登录时，再次跳转到登录界面。
 *
 *
 *2.实现逻辑：获取session中的用户对象
 *	1) 若未获取到，则说明用户首次访问该程序，未登录
 *	2) 若获取到，则说明用户已进行登录，直接访问主页
 * 
 */

package OSMS.action.Boss;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Boss;
import OSMS.entity.Goods;


public class Boss_Index_Servlet extends HttpServlet {
	
	GoodsDao gdao = new GoodsDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
//---------------------------1. 获取session检验是否首次访问 ----------------------------//
		HttpSession session = request.getSession();
		Object obj_boss = null;
		obj_boss = session.getAttribute("boss");
		Boss boss = (Boss)obj_boss;
		String bid = Integer.toString(boss.getBid());
		ArrayList<Goods> goodsList = new ArrayList<Goods>();
		try {
			goodsList = (ArrayList<Goods>) gdao.getGoodsByBid(bid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("goodsList", goodsList);
		
		if(obj_boss != null) {//boss非空，则说明非首次访问，直接访问系统首页
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else{			 //boss为空，则说明首次访问，跳转至登录界面
			request.getRequestDispatcher("bosslogin.html").forward(request, response);
		}
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
